package com.cx.algrithm.sort;

/**
 * @author shkstart
 * @date 7/22/2019 - 8:22 AM
 */
public class BubbleSort {


    public static void bubble(int[] a) {
//        從小到大排序
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = 0; j < a.length - i - 1; j++) {
                if (a[j] > a[j + 1]) {
                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
        for (int i1 : a) {
            System.out.print(i1 + " ");
        }
//        另一種方式
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = i + 1; j < a.length; j++) {
                if (a[i] > a[j]) {
                    int temp = a[j];
                    a[j] = a[i];
                    a[i] = temp;
                }
            }
        }
        System.out.println();
        for (int i1 : a) {
            System.out.print(i1 + " ");
        }
    }

    public static void bubble2(int[] a) {
        int i, j;
        for (i = 0; i < a.length - 1; i++) {
            for (j = 0; j < a.length - i - i; j++) {
                if (a[j] > a[j + 1]) {
                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
    }


    public static void main(String[] args) {
        int[] a = {23, 1, 34, 21, 46, 55, 32, 43};
        bubble(a);
//         bubble2(a);
    }
}


